package com.itany.corejava.day15;

import javax.swing.*;

public class TimeoutFrame {

    private JFrame jf;
    private JLabel jl;

    private String title;
    private int x;
    private int y;
    private int width;
    private int height;




    public TimeoutFrame(String title, int x, int y, int width, int height) {
        this.title = title;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;

        jf=new JFrame(title+"倒计时牌");
        jl=new JLabel("时间到");
    }

    private void init(){
        jf.add(jl);
    }


    private void addEventHandler(){
        new Thread(){
            @Override
            public void run() {

                int x=jf.getX();
                int y=jf.getY();

                for (int i = 0; i < 40; i++) {
                    if(i%2==0){
                        x+=3;
                        y+=3;
                    }else{
                        x-=3;
                        y-=3;
                    }

                    jf.setLocation(x,y);

                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }



            }
        }.start();
    }


    public void show(){
        init();
        addEventHandler();
        jf.setSize(width,height);
        jf.setLocation(x,y);
        jf.setExtendedState(JFrame.NORMAL);
        jf.setAlwaysOnTop(true);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
